package com.lanxiao.cloud.common.core.utils;

import java.util.List;
import java.util.Objects;

/**
 * @author ：qiaoliang
 * @date ：2021-10-20
 */
public class PageUtils {

  /**
   * 开始分页
   * @param list
   * @param pageNum 页码
   * @param pageSize 每页多少条数据
   * @return
   */
  public static <T> List<T> startPage(List<T> list, Integer pageNum,
      Integer pageSize) {
    if (list == null) {
      return null;
    }
    if (list.size() == 0) {
      return null;
    }
    // 记录总数
    Integer count = list.size();
    // 页数
    Integer pageCount = 0;
    if (count % pageSize == 0) {
      pageCount = count / pageSize;
    } else {
      pageCount = count / pageSize + 1;
    }
    // 开始索引
    int fromIndex = 0;
    // 结束索引
    int toIndex = 0;

    if (!Objects.equals(pageNum, pageCount)) {
      fromIndex = (pageNum - 1) * pageSize;
      toIndex = fromIndex + pageSize;
    } else {
      fromIndex = (pageNum - 1) * pageSize;
      toIndex = count;
    }

    return list.subList(fromIndex, toIndex);
  }

}
